package com.smartsaas.ugclivii.cd;

import com.smartsaas.ugclivii.job.JobUtils;
import com.smartsaas.ugclivii.ui.UiContext;
import com.smartsaas.ugclivii.utils.FunUtils;
import nxopen.*;
import nxopen.blockstyler.Node;
import nxopen.blockstyler.Tree;
import nxopen.uf.UFView;

import java.rmi.RemoteException;

public class MlTreeSelectHandler implements Tree.OnSelectCallback{
    @Override
    public void onSelectCallback(Tree tree, Node node, int i, boolean b) throws NXException, RemoteException {
        UiContext.getInstance().debug("i:"+i+" b:"+b);
        if (b){
            JobUtils.doJob("选择模料",()->{
                selectBody(node);
            });
        }
    }
    private static Body CURRENT_BODY=null;
    private static Node CURRENT_NODE=null;
    public static void selectBody(Node node) {
        try {
            String tag = node.getColumnDisplayText(2);
            BodyCollection bodies = UiContext.SESSION.parts().work().bodies();
            TaggedObjectCollection.Iterator iterator = bodies.iterator();
            UFView ufView = UiContext.UF_SESSION.view();
            while (iterator.hasNext()){
                Body next = (Body)iterator.next();
                if (String.valueOf(next.tag().value).equals(tag)){
                    next.unhighlight();
                    next.unblank();
                    CURRENT_BODY=next;
                    CURRENT_NODE=node;
                    ufView.fitView(Tag.NULL, 0.5f);
                }else {
                    next.blank();
                }
            }
        }catch (Exception e){
            UiContext.getInstance().debug(e);
        }
    }

    public static void selectBody(String tag) {
        try {
            BodyCollection bodies = UiContext.SESSION.parts().work().bodies();
            TaggedObjectCollection.Iterator iterator = bodies.iterator();
            UFView ufView = UiContext.UF_SESSION.view();
            while (iterator.hasNext()){
                Body next = (Body)iterator.next();
                if (String.valueOf(next.tag().value).equals(tag)){
                    next.unhighlight();
                    next.unblank();
                    ufView.fitView(Tag.NULL, 0.5f);
                }else {
                    next.blank();
                }
            }
        }catch (Exception e){
            UiContext.getInstance().debug(e);
        }
    }

    public static void getBodyDistances(){
        //量尺逻辑
        synchronized (MlTreeSelectHandler.class){
            UiContext instance = UiContext.getInstance();
            try {
                if (CURRENT_NODE!=null&&CURRENT_BODY!=null){
                    double[] distances = FunUtils.getBodyDistances(instance.getMlCsy(),CURRENT_BODY);
                    double rx = instance.getMlrx().value();
                    double ry = instance.getMlry().value();
                    double rz = instance.getMlrz().value();
                    if (distances!=null){
                        CURRENT_NODE.setColumnDisplayText(3,String.valueOf((int) distances[0]));
                        CURRENT_NODE.setColumnDisplayText(4,String.valueOf((int)distances[1]));
                        CURRENT_NODE.setColumnDisplayText(5,String.valueOf((int)distances[2]));
                        CURRENT_NODE.setColumnDisplayText(6,String.valueOf((int)distances[0]+2*rx));
                        CURRENT_NODE.setColumnDisplayText(7,String.valueOf((int)distances[1]+2*ry));
                        CURRENT_NODE.setColumnDisplayText(8,String.valueOf((int)distances[2]+2*rz));
                    }
                }
                CURRENT_NODE=null;
                CURRENT_BODY=null;
            }
            catch (Exception e){
                instance.debug(e);
            }
        }
    }
}
